//
//  HRZConfirmOrderViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/21.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZConfirmOrderViewController.h"


// Controllers

// Models
#import "HRZStoreModel.h"
#import "HRZGoodsModel.h"
// Views
#import "HRZAcceptAddressCell.h"
#import "HRZAddAcceptAddressCell.h"

//#import "HRZ_ShoppingCartCell.h"
#import "HRZShoppingCartHeaderView.h"

#import "HRZSendTypeCell.h"
#import "HRZLeaveMessageCell.h"
#import "HRZTotalCountCell.h"

#import "HRZWriteOutInvoiceCell.h"

#import "HRZConfirmOrderBottomView.h"
// Vendors
#import <MJExtension/MJExtension.h>
// Categories
#import "HRZAddressDataBase.h"

// Others

@interface HRZConfirmOrderViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 底部view */
@property (nonatomic, strong) HRZConfirmOrderBottomView *bottomView;
/** 门店集合 */
@property (nonatomic, strong) NSMutableArray <HRZStoreModel*>*storesArray;

@end

static NSString * const HRZAcceptAddressCellID          = @"HRZAcceptAddressCell";
static NSString * const HRZAddAcceptAddressCellID       = @"HRZAddAcceptAddressCell";
static NSString * const HRZ_ShoppingCartCellID          = @"HRZ_ShoppingCartCell";
static NSString * const HRZ_ShoppingCartHeaderViewID    = @"HRZ_ShoppingCartHeaderView";
static NSString * const HRZSendTypeCellID               = @"HRZSendTypeCell";
static NSString * const HRZLeaveMessageCellID           = @"HRZLeaveMessageCell";
static NSString * const HRZTotalCountCellID             = @"HRZTotalCountCell";
static NSString * const HRZWriteOutInvoiceCellID        = @"HRZWriteOutInvoiceCell";
//static NSString * const HRZAcceptAddressCellID = @"HRZAcceptAddressCell";


@implementation HRZConfirmOrderViewController

@synthesize tableView = _tableView;

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - HRZTopNavH - 60) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = HRZBGColor;
        _tableView.estimatedRowHeight = 150;//估算高度
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        
        /** 注册 */
        [_tableView registerNib:[UINib nibWithNibName:@"HRZAcceptAddressCell" bundle:nil] forCellReuseIdentifier:HRZAcceptAddressCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZAddAcceptAddressCell" bundle:nil] forCellReuseIdentifier:HRZAddAcceptAddressCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZSendTypeCell" bundle:nil] forCellReuseIdentifier:HRZSendTypeCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZLeaveMessageCell" bundle:nil] forCellReuseIdentifier:HRZLeaveMessageCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZTotalCountCell" bundle:nil] forCellReuseIdentifier:HRZTotalCountCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZWriteOutInvoiceCell" bundle:nil] forCellReuseIdentifier:HRZWriteOutInvoiceCellID];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRZ_ShoppingCartCell" bundle:nil] forCellReuseIdentifier:HRZ_ShoppingCartCellID];
        
        [_tableView registerClass:[HRZShoppingCartHeaderView class] forHeaderFooterViewReuseIdentifier:HRZ_ShoppingCartHeaderViewID];
        
    }
    return _tableView;
}

- (HRZConfirmOrderBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"HRZConfirmOrderBottomView" owner:self options:nil] firstObject];
        _bottomView.frame = CGRectMake(0, kScreenHeight - HRZTopNavH - 40, kScreenWidth, 60);
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

- (NSMutableArray<HRZStoreModel *> *)storesArray {
    if (!_storesArray) {
        _storesArray = [[NSMutableArray alloc] init];
    }
    return _storesArray;
}

#pragma mark -  scycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpData];
    
    [self setUpdateUI];
    
}

- (void)setUpData{
    
    self.storesArray = [HRZStoreModel mj_objectArrayWithFilename:@"Store001.plist"];
    
}

- (void)setUpdateUI {
    self.title = @" 确认订单";
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.bottomView];
    
    self.bottomView.storeArray = self.storesArray;
    self.bottomView.submitBtnClick = ^{
        NSLog(@"==提交");
    };
}

- (void)setSelectedGoodsArray:(NSArray<HRZGoodsModel *> *)selectedGoodsArray {
    _selectedGoodsArray = selectedGoodsArray;
    
    for (HRZGoodsModel *goodsModel in selectedGoodsArray) {
        BOOL isNewStore = YES;
        //  已存在门店是否包含商品
        for (HRZStoreModel *storeModel in self.storesArray) {
//            if ([goodsModel.shopId isEqualToString:storeModel.id] && [goodsModel.storeName isEqualToString:storeModel.storeName]) {
//                [storeModel.goodsArray addObject:goodsModel];
//                isNewStore = NO;
//            }
        }
        
        //  商品属于新门店
        if (isNewStore) {
            HRZStoreModel *storeModel = [[HRZStoreModel alloc] init];
            [storeModel.goodsArray addObject:goodsModel];
//            storeModel.id = goodsModel.shopId;
            storeModel.storeName = goodsModel.storeName;
            
            [self.storesArray addObject:storeModel];
        }
    }
    
    [_tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - <UITabelViewDataSorce>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.storesArray.count + 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == _storesArray.count + 1) {
        return 1;
    } else {
        HRZStoreModel *store = _storesArray[section - 1];
        return store.goodsArray.count + 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    
    if (indexPath.section == 0) {   /** 收获地址 */
        if (/* DISABLES CODE */ (1)) {
            HRZAddressItem * addressItem = [[HRZAddressDataBase sharedDataBase] defaultAddress];
            HRZAcceptAddressCell *accepCell = [tableView dequeueReusableCellWithIdentifier:HRZAcceptAddressCellID];
            accepCell.addressItem = addressItem;
            cell = accepCell;
        } else {
            HRZAddAcceptAddressCell *addAdrCell = [tableView dequeueReusableCellWithIdentifier:HRZAddAcceptAddressCellID];
            cell = addAdrCell;
        }
    } else if (indexPath.section == self.storesArray.count + 1){    /** 发票 */
        HRZWriteOutInvoiceCell *invoiceCell = [tableView dequeueReusableCellWithIdentifier:HRZWriteOutInvoiceCellID];
        cell = invoiceCell;
    } else {    /** 门店商品 */
        HRZStoreModel *storeMdel = _storesArray[indexPath.section - 1];
        int goodsCount = (int)storeMdel.goodsArray.count;
        if (indexPath.row == goodsCount) {  /** 配送方式 */
            HRZSendTypeCell *sendCell = [tableView dequeueReusableCellWithIdentifier:HRZSendTypeCellID];
            cell = sendCell;
        } else if (indexPath.row == goodsCount + 1){    /** 留言 */
            HRZLeaveMessageCell *messageCell = [tableView dequeueReusableCellWithIdentifier:HRZLeaveMessageCellID];
            messageCell.LeaveMessageBlock = ^(NSString *message) {
                NSLog(@"%@", message);
            };
            cell = messageCell;
        } else if (indexPath.row == goodsCount + 2) {   /** 门店总计 */
            HRZStoreModel *storeModel = self.storesArray[indexPath.section - 1];
            HRZTotalCountCell *totalCell = [tableView dequeueReusableCellWithIdentifier:HRZTotalCountCellID];
            totalCell.storeModel = storeModel;
            cell = totalCell;
        } else {    /** 商品 */
//            HRZStoreModel *storeModel = self.storesArray[indexPath.section - 1];
//            HRZGoodsModel *goodesModel = storeModel.goodsArray[indexPath.row];
            
//            HRZ_ShoppingCartCell *cartCell = [tableView dequeueReusableCellWithIdentifier:HRZ_ShoppingCartCellID];
//            cartCell.goodsModel = goodesModel;
//
//            [cartCell updateCurrentConstraints];
//
//            cell = cartCell;
        }
    }
    
    return cell;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = nil;
    
    if (section != 0 && section != self.storesArray.count + 1) {
        HRZShoppingCartHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HRZ_ShoppingCartHeaderViewID];
        headerView.contentView.backgroundColor = [UIColor whiteColor];

        HRZStoreModel *storeModel = self.storesArray[section - 1];

        headerView.storeModel = storeModel;

        [headerView updateCurrentConstraints];

        view = headerView;
    }
    
    return view;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = nil;
    if (section == 0) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        view.backgroundColor = HRZBGColor;
    } else if (section == self.storesArray.count + 1) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        view.backgroundColor = HRZBGColor;
    } else {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
        view.backgroundColor = HRZBGColor;
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0 && indexPath.section != self.storesArray.count + 1) {
        
        if (indexPath.row < self.storesArray[indexPath.section - 1].goodsArray.count) {
            return 90;
        } else {
            return tableView.rowHeight;
        }
    }
    return tableView.rowHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == self.storesArray.count + 1){
        return 0;
    } else {
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSLog(@"");
    } else if (indexPath.section == self.storesArray.count + 1) {
        NSLog(@"");
    }
}

@end
